<?php
/**
 * 文件名：brandindex.mod.php
 * 版本号：1.0
 * 最后修改时间：2006年7月18日 21:00:05
 * 作者: MrAsong
 * 功能描述：品牌模块
 */
if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject
{
	
	var $ID = 0;


	
	var $ImagePath;
	
	var $sms_register = null;


	
	function ModuleObject($config)
	{
		$this->MasterObject($config);

		if(isset($this->Get['id']))
		{
			$this->ID = (int)$this->Get['id'];
		}elseif(isset($this->Post['id']))
		{
			$this->ID = (int)$this->Post['id'];
		}

		$_GET['rmod']='my';
		if(MEMBER_ID > 0) {
			$this->IsAdmin = $this->MemberHandler->HasPermission('member','admin');
		}		

		$this->Execute();
	}

	
	function Execute()
	{
	
		$__my=$this->MemberHandler->MemberFields;
		define('_COM_NAME_',$__my['danwei']);
		define('_IS_BRAND_ADMIN_',$__my['gbquanx']);
		ob_start();
		switch($this->Code)
		{

			case 'activities':
				$this->activities();
				break;
				
			case 'activities_edit':
				$this->activities_edit();
				break;
				
			case 'activities_del':
				$this->activities_del();
				break;
				
			case 'brand':
				$this->brand();
				break;
				
			case 'brand_edit':
				$this->brand_edit();
				break;
				
			case 'brand_del':
				$this->brand_del();
				break;
				
			case 'category':
				$this->category();
				break;	
				
			case 'category_edit':
				$this->category_edit();
			break;	
				
			case 'category_del':
				$this->category_del();
			break;	
			
			default:
				$this->Main();
				
				break;
		}
		$Contents=ob_get_clean();
		$this->ShowBody($Contents);
	}

	function Main(){ //DEFAULT
		$this->category();
	}
	
	//分类列表
	function category(){
		$where = " where company = '"._COM_NAME_."'";
		$per_page_num = min(500,max(10,(int) $_GET['page'],10));
		$query_link = 'index.php?mod=brandadmin&code=category';
		$sql = " select count(*) as `total_brand` from `".TABLE_PREFIX."brandclass` $where ";	
		$query = $this->DatabaseHandler->Query($sql);
		extract($query->GetRow());
		$page_arr = pagebrand($total_brand,$per_page_num,$query_link,array('return'=>'array',));

	  
	    $sql = "select * from `".TABLE_PREFIX."brandclass`  $where  {$page_arr['limit']} ";
	    $query = $this->DatabaseHandler->Query($sql);
	    while ($row = $query->GetRow()){
			$navbrand[] = $row;
		}
	   include(ROOT_PATH . './templates/app/brand_admin_category.php');	
	}
	
	//分类添加
	function category_edit(){

		if( isset($this->Get['action']) && $this->Get['action']=="edit"  ){//保存数据
			$brand_title = $this->Post['brand_title'];
			$brand_desc = $this->Post['brand_desc'];
			$brand_com = _COM_NAME_;
			
			if( isset($this->Get['id']) && !empty($this->Get['id']) ){
				$sql = "update `".TABLE_PREFIX."brandclass` set company = '$brand_com', brandseri='$brand_title', tobacconame='$brand_desc' where id={$this->Get['id']} ";
				$html = '<script type="text/javascript">alert("更新成功！");location.href="index.php?mod=brandadmin&code=category";</script>';
			}else{
				$sql = "insert into `".TABLE_PREFIX."brandclass` (company,brandseri,tobacconame) values('$brand_com','$brand_title','$brand_desc')";
				
				$html = '<script type="text/javascript">alert("添加成功！");location.href="index.php?mod=brandadmin&code=category";</script>';
			}
			$this->DatabaseHandler->Query($sql);
			echo $html;			
		}else{	
			if( isset($this->Get['id']) && !empty($this->Get['id']) ){
				$sql = "select * from `".TABLE_PREFIX."brandclass` where id =  {$this->Get['id']} ";
				$query = $this->DatabaseHandler->Query($sql);
				$brandInfo = $query->GetRow();
			}		
		}
		

	    include(ROOT_PATH . './templates/app/brand_admin_category_edit.php');	
	}
	
	function category_del(){
		if(isset($this->Get['id']) && !empty($this->Get['id'])){
			$sql = "delete from " . TABLE_PREFIX . "brandclass where id=".$this->Get['id'];
			$query = $this->DatabaseHandler->Query($sql);
			$html = '<script type="text/javascript">alert("删除成功！");location.href="index.php?mod=brandadmin&code=category";</script>';
			echo $html;	
		}	
	}
	
	

	//品牌列表
	function brand(){

		$query_link = 'index.php?mod=brandadmin&code=brand';
		$sql = "select count(*) as `total_tobacco` from `".TABLE_PREFIX."tobacco` where seriesID in( select id from `".TABLE_PREFIX."brandclass` where company = '"._COM_NAME_."')";
		$query = $this->DatabaseHandler->Query($sql);
        extract($query->GetRow());
		$page_arr = pagebrand($total_tobacco,10,$query_link,array('return'=>'array',));
		
		$sql = "select a.*,b.brandseri from `jy_tobacco` as a left join  `jy_brandclass` as b on a.seriesID=b.id where a.seriesID in  ( select id from `jy_brandclass` where company = '"._COM_NAME_."')  {$page_arr['limit']}";
		
	 	$query = $this->DatabaseHandler->Query($sql);
	     $tobacco_list=array(); 
		while($row = $query->GetRow())
		{	
			$tobacco_list[]=$row;
		}
	
	   include(ROOT_PATH . './templates/app/brand_admin_brand.php');	
	}

	//品牌添加	
	function brand_edit(){
	
		if( isset($this->Get['action']) && $this->Get['action']=="edit"  ){//保存数据
			$tb_title = $this->Post['tb_title'];
			$tb_w_price = $this->Post['tb_w_price'];
			$tb_r_price = $this->Post['tb_r_price'];
			$tb_standard = $this->Post['tb_standard'];
			$tb_package = $this->Post['tb_package'];
			$tb_hbarcode = $this->Post['tb_hbarcode'];
			$tb_tbarcode = $this->Post['tb_tbarcode'];
			$tb_intro = $this->Post['tb_intro'];
			$tb_pinlei = $this->Post['tb_pinlei'];
			$tb_producttype = $this->Post['tb_producttype'];
			$tb_seriesID = $this->Post['tb_seriesID'];
			
			
			if( empty($this->Post['tb_pic']) ){
				$tb_pic = "";
				$tb_uppic = "";
				$tb_morePic = "";				
			}else{
				$pic_str = trim($this->Post['tb_pic']);
				$pics = explode("|",trim($pic_str) , 2 );
				$tb_pic = $pics[0];
				$tb_morePic = "";
				$tb_uppic = "pic = '$tb_pic' ,";

				if( count($pics) > 1 ){
					$tb_morePic = $pics[1];
				}

			}
			
			if( isset($this->Get['id']) && !empty($this->Get['id']) ){
				$sql = "update `".TABLE_PREFIX."tobacco` set 
					tobacco_name = '$tb_title',
					w_price = $tb_w_price ,
					r_price = $tb_r_price ,
					standard = '$tb_standard' ,
					package = '$tb_package' ,
					hBarcode = '$tb_hbarcode' ,
					tBarcode = '$tb_tbarcode' ,
					intro = '$tb_intro' ,
					pinlei = '$tb_pinlei' ,
					producttype = '$tb_producttype' , 
					$tb_uppic 
					seriesID = $tb_seriesID 
				where id={$this->Get['id']} ";
				$this->DatabaseHandler->Query($sql);
				if( !empty($tb_morePic) ){
					$sqla = "select count(*) as `morepic_count` from  `".TABLE_PREFIX."morepic` where tobaccoid = {$this->Get['id']} ";
					$query = $this->DatabaseHandler->Query($sqla);
					extract($query->GetRow());
					if( $morepic_count > 0 ){
						$sql = "update `".TABLE_PREFIX."morepic` set 
							tobpic = '$tb_morePic' 
							where tobaccoid = {$this->Get['id']} ";
							echo $sql;
						$this->DatabaseHandler->Query($sql);					
					}else{
						$sql = "insert into `".TABLE_PREFIX."morepic` 
							( tobpic, tobaccoid) values ('$tb_morePic', ".$this->Get['id'].")";
						$this->DatabaseHandler->Query($sql);
					}
				}
				$html = '<script type="text/javascript">alert("更新成功！");location.href="index.php?mod=brandadmin&code=brand";</script>';
			}else{
				$sql = "insert into `".TABLE_PREFIX."tobacco` 
					( tobacco_name ,w_price ,r_price ,standard ,package ,hBarcode ,tBarcode ,intro ,pinlei ,producttype, pic, seriesID) values
					( '$tb_title',$tb_w_price ,$tb_r_price ,'$tb_standard' ,'$tb_package' ,'$tb_hbarcode' ,'$tb_tbarcode' ,'$tb_intro' ,'$tb_pinlei' ,'$tb_producttype', '$tb_pic', $tb_seriesID)";
				$this->DatabaseHandler->Query($sql);

				if( !empty($tb_morePic) ){
					$sql = "insert into `".TABLE_PREFIX."morepic` 
						( tobpic, tobaccoid) values ('$tb_morePic', ".mysql_insert_id().")";
					$this->DatabaseHandler->Query($sql);
				}
				
				$html = '<script type="text/javascript">alert("添加成功！");location.href="index.php?mod=brandadmin&code=brand";</script>';
			}
			
			//die($sql);
			echo $html;	
		}else{	
			$where = " where company = '"._COM_NAME_."'";
			$sql = "select * from `".TABLE_PREFIX."brandclass`  $where";
			$query = $this->DatabaseHandler->Query($sql);
			while ($row = $query->GetRow()){
				$comBrandList[] = $row;
			}
		
			if( isset($this->Get['id']) && !empty($this->Get['id']) ){
				$sql = "select * from `".TABLE_PREFIX."tobacco` where id =  {$this->Get['id']} ";
				$query = $this->DatabaseHandler->Query($sql);
				$tbInfo = $query->GetRow();
			}
			

			
		}
	
	
	   include(ROOT_PATH . './templates/app/brand_admin_brand_edit.php');	
	}
	
	function brand_del(){
		if(isset($this->Get['id']) && !empty($this->Get['id'])){
			$sql = "delete from " . TABLE_PREFIX . "tobacco where id=".$this->Get['id'];
			$query = $this->DatabaseHandler->Query($sql);
			$html = '<script type="text/javascript">alert("删除成功！");location.href="index.php?mod=brandadmin&code=brand";</script>';
			echo $html;	
		}
	}
	
	
	
	//活动列表
	function activities(){
	   include(ROOT_PATH . './templates/app/brand_admin_activities.php');	
	}	
	
	//活动添加
	function activities_edit(){
	   $savename ='';
	   if(empty($this->Post['id'])){
	   if($image=$_FILES['pic']['name']){
	     $filename = date('YmdHis');
		  $savename=$filename.strtolower(strstr($_FILES['pic']['name'], '.'));
		  $s_pic= dirname(dirname(__FILE__))."/images/event/".$savename;
		   if(is_uploaded_file($_FILES['pic']['tmp_name'])){
             if(!move_uploaded_file($_FILES['pic']['tmp_name'], $s_pic)){
             echo '移动文件失败！';
              exit;
          }
         }else{
           echo 'problem!';
           exit;
         }
		 }
	   $sql = "insert into `".TABLE_PREFIX."event` ( title ,fromt ,tot ,content ,image ) values
					( '{$this->Post['title']}','{$this->Post['fromt']}' ,'{$this->Post['tot']}' ,'{$this->Post['content']}' ,'$savename')";
		
		$this->DatabaseHandler->Query($sql);
	   }
	   include(ROOT_PATH . './templates/app/brand_admin_activities_edit.php');	
	}	
	
	
	function pager($total,$page,$pagesize,$url){
		$page = (intval($page)>1) ? intval( $page ) : 1 ;
		$offSet = ($page-1)*$pagesize ;
		$pages = ceil($total/$pagesize) ;
		$pagePre = $pageNext = $url;
		if( $page > 1 ){
			$pagePre = $pagePre."&page=".$page-1;
		}
		
		if( $page < $pages ){
			$pageNext = $pageNext."&page=".$page+1;
		}
		
		return array(
			'total'    => $total,
			'page'     => $page,
			'offSet'   => $offSet,
			'pages'    => $pages,
			'pagePre'  => $pagePre,
			'pageNext' => $pageNext,
			'limit'    => ' limit '.$offSet.','.$pagesize 
		);
	}	
	
}

?>
